package com.yxhl.zoume.core.coupon.fragment;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.fragment.BaseDataBindingFragment;
import com.yxhl.zoume.core.coupon.adapter.HistoryCouponAdapter;
import com.yxhl.zoume.core.coupon.presenter.HistoryCouponPresenter;
import com.yxhl.zoume.core.coupon.view.HistoryCouponView;
import com.yxhl.zoume.data.http.model.coupon.CouponInfo;
import com.yxhl.zoume.di.component.coupon.DaggerHistoryCouponComponent;
import com.yxhl.zoume.di.component.coupon.HistoryCouponComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.utils.DensityUtils;
import com.yxhl.zoume.utils.PromptUtils;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by vhawk on 2016/11/3.
 */
public class HistoryCouponFragment extends BaseDataBindingFragment implements HistoryCouponView {

    @BindView(R.id.history_coupon_recycle_view)
    RecyclerView historyCouponRecycleView;
    @Inject
    HistoryCouponPresenter presenter;
    private ViewDataBinding historyDatabinding;
    private HistoryCouponComponent component;
    private HistoryCouponAdapter adapter;

    public static Fragment newInstance() {
        Fragment fragment = new HistoryCouponFragment();
        return fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.attachView(this);
        initRecycleView();
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        presenter.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        presenter.onDestroy();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser){
            presenter.fetchHistoryCoupon();
        }
    }

    private void initRecycleView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(component.context());
        adapter = new HistoryCouponAdapter();
        historyCouponRecycleView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(DensityUtils.dip2px(component.context(), 8),
                        DensityUtils.dip2px(component.context(), 6),
                        DensityUtils.dip2px(component.context(), 8),
                        DensityUtils.dip2px(component.context(), 6));

            }
        });
        historyCouponRecycleView.setLayoutManager(layoutManager);
        historyCouponRecycleView.setAdapter(adapter);
    }

    @Override
    protected void initializeInjector() {
        component = DaggerHistoryCouponComponent.builder()
                .appComponent(getAppComponent())
                .activityModule(new ActivityModule(getActivity()))
                .build();
        component.inject(this);
    }

    @Override
    protected View getContentLayout(LayoutInflater inflater, ViewGroup container) {
        historyDatabinding = DataBindingUtil.inflate(inflater, R.layout.fragment_coupon_history, container, false);
        return historyDatabinding.getRoot();
    }

    @Override
    protected void setupToolBar() {

    }

    @Override
    public void handleFetchHistoryCouponSucceed(List<CouponInfo> couponInfoList) {
        adapter.setHistoryCoupons(couponInfoList);
    }

    @Override
    public void handleFetchHistoryCouponFailure(String msg) {
        PromptUtils.showShort(component.context(), msg);
    }

    @Override
    public void handleFetchHistoryCouponError() {
        PromptUtils.showShort(component.context(), R.string.net_error);
    }
}
